package com.lancoo.znbkxx.teacher.model.repository


import com.google.gson.Gson
import com.lancoo.cpbase.authentication.base.CurrentUser
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.Ext.Companion.BASE_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.ONEONONE_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.SCHOOLPAPER_URL
import com.lancoo.znbkxx.teacher.Ext.Companion.STAGE_NO
import com.lancoo.znbkxx.teacher.model.api.BaseRepository
import com.lancoo.znbkxx.teacher.model.api.ZnbkRetrofitClient
import com.lancoo.znbkxx.teacher.model.data.*
import com.lancoo.znbkxx.teacher.service.PaperService
import com.lancoo.znbkxx.teacher.service.SystemService
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody

/**
 * @author : Admin
 * @date   : 2019/7/22 21:42
 * @desc   :
 */
class PaperRepository : BaseRepository() {
    suspend fun getPaperForMobileSearch(UserID: String, Token: String, SchoolID: String,
                                        CourseClassID: String, Layer: String, SearchInfo: String,
                                        LowDateTime: Long,
                                        HighDateTime: Long, State: String, PageNumber: Int,
                                        PageSize: Int): ZnbkResponse<SearchPaperList> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getPaperForMobileSearch(UserID, Token, SchoolID, CourseClassID, Layer,
                    SearchInfo, LowDateTime, HighDateTime, State, PageNumber, PageSize)
        }
    }

    suspend fun getPaperSetListForMobile(UserID: String, Token: String, SchoolID: String,
                                         CourseClassID: String, Layer: Int, PageNumber: Int,
                                         PageSize: Int): ZnbkResponse<PaperList> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getPaperSetListForMobile(UserID, Token, SchoolID, CourseClassID, Layer,
                    PageNumber, PageSize)
        }
    }

    suspend fun getPaperSetForMobileTerminal(Key: String, SubjectID: String, GlobalGrade: String,
                                             CourseClassID: String, TID: String, SchoolID: String,
                                             Layer: Int, SearchInfo: String,
                                             State: String, DateField: String, PageNumber: Int,
                                             PageSize: Int): ZnbkResponse<PaperList> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getPaperSetForMobileTerminal(Key, SubjectID, GlobalGrade, CourseClassID, TID,
                    SchoolID, Layer, SearchInfo, State, DateField, PageNumber, PageSize)
        }
    }

    suspend fun getPaperForMobileSearch(Key: String, SubjectID: String, GlobalGrade: String,
                                        CourseClassID: String, TID: String, SchoolID: String,
                                        Layer: String, SearchInfo: String,
                                        State: String, DateField: String, PageNumber: Int,
                                        PageSize: Int): ZnbkResponse<SearchPaperList> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getPaperForMobileSearch(Key, SubjectID, GlobalGrade, CourseClassID, TID,
                    SchoolID, Layer, SearchInfo, State, DateField, PageNumber, PageSize)
        }
    }

    suspend fun releaseAuditPaper(SetID: String, GlobalGrade: String, Token: String,
                                  PaperList: MutableList<String>, StartTime: String,
                                  EndTime: String,
                                  ClassList: MutableList<ClassFlag>, UserID: String,
                                  SchoolID: String, Layer: Int,
                                  PaperLibraryType: Int,
                                  TeacherName: String): ZnbkResponse<ReleaseInfo> {
        val map = mutableMapOf<String, Any>()
        map["SetID"] = SetID
        map["GlobalGrade"] = GlobalGrade
        map["PaperList"] = PaperList
        map["StartTime"] = StartTime
        map["EndTime"] = EndTime
        map["ClassList"] = ClassList
        map["SchoolID"] = SchoolID
        map["Layer"] = Layer
        if (Layer == 5) {
            map["PaperLibraryType"] = PaperLibraryType
            map["TeacherName"] = TeacherName
        }
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        val requestBody = Gson().toJson(map).toRequestBody(mediaType)
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request { service.releaseAuditPaper(Token, UserID, requestBody) }
    }

    suspend fun getDataAnalyze(CourseClassID: String, Key: String, SchoolID: String, token: String,
                               userID: String): ZnbkResponse<AnalysisInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request { service.getDataAnalyze(CourseClassID, Key, SchoolID, token, userID) }
    }

    suspend fun setPaperAnswerDuration(PaperID: String, CourseClassID: String, StartTime: String,
                                       EndTime: String, Token: String, SchoolID: String,
                                       SetID: String, GlobalGrade: String,
                                       UserID: String): ZnbkResponse<Boolean> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.setPaperAnswerDuration(PaperID, CourseClassID, StartTime, EndTime, Token,
                    SchoolID, SetID, GlobalGrade, UserID)
        }
    }

    suspend fun getStuProgress(PaperID: String, CourseClassID: String, SchoolID: String,
                               Token: String,
                               UserID: String): ZnbkResponse<MutableList<StuProgressInfo>> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getStuProgress(PaperID, CourseClassID, SchoolID, Token, UserID)
        }
    }

    suspend fun getGradeClassWithPaperState(GlobalGrade: String, SchoolID: String, SetID: String,
                                            PaperID: String, Token: String,
                                            UserID: String): ZnbkResponse<MutableList<ClassFlag>> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getGradeClassWithPaperState(GlobalGrade, SchoolID, SetID, PaperID, Token,
                    UserID)
        }
    }

    suspend fun previewStuHomeworkInfo(Token: String, UserID: String, StuID: String,
                                       SchoolID: String,
                                       Layer: Int): ZnbkResponse<PreviewStuHomeworkInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, ONEONONE_URL)
        return request {
            service.previewStuHomeworkInfo(Token, UserID, StuID, SchoolID, Layer)
        }
    }

    suspend fun getStuNewTask(Token: String, UserID: String, PageIndex: Int,
                              PageSize: Int): ZnbkResponse<MutableList<NewTask>> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getStuNewTask(Token, UserID, PageIndex, PageSize)
        }
    }

    suspend fun getPaperBaseInfoList(Token: String, UserID: String, StuID: String, SchoolID: String,
                                     Layer: Int, PageIndex: Int, PageSize: Int,
                                     State: Int): ZnbkResponse<PaperBaseInfoList> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, ONEONONE_URL)
        return request {
            service.getPaperBaseInfoList(Token, UserID, StuID, SchoolID, Layer, PageIndex, PageSize,
                    State, true)
        }
    }

    suspend fun getTeacherInfo(Token: String, UserID: String): ZnbkResponse<TeacherInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, ONEONONE_URL)
        return request {
            service.getTeacherInfo(Token, UserID)
        }
    }

    suspend fun getHomeworkStatisticAnalysis(Token: String, UserID: String, StuID: String,
                                             SchoolID: String): ZnbkResponse<MutableList<HomeWorkAnalysis>> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, ONEONONE_URL)
        return request {
            service.getHomeworkStatisticAnalysis(Token, UserID, StuID, SchoolID)
        }
    }

    suspend fun getStuRZInfo(Token: String, UserID: String, StuID: String,
                             SchoolID: String): ZnbkResponse<RZInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, ONEONONE_URL)
        return request {
            service.getStuRZInfo(Token, UserID, StuID, SchoolID)
        }
    }

    suspend fun doHastenCallOneOnOne(Token: String, UserID: String, StuID: String, PaperID: String,
                                     SchoolID: String): ZnbkResponse<Int> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, ONEONONE_URL)
        return request {
            service.doHastenCallOneOnOne(Token, UserID, StuID, PaperID, SchoolID)
        }
    }

    suspend fun doHastenCall(Token: String, UserID: String, PaperID: String, SetID: String,
                             CourseClassID: String, PaperName: String,
                             SchoolID: String): ZnbkResponse<Int> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.doHastenCall(Token, UserID, PaperID, SetID, CourseClassID, PaperName, SchoolID)
        }
    }

    suspend fun getEffectivenessEvaluation(Token: String, UserID: String,
                                           CourseClassID: String): ZnbkResponse<PaperEffectiveInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getEffectivenessEvaluation(Token, UserID, CourseClassID)
        }
    }

    suspend fun getMobilePaperList(CourseClassID: String, IsRelease: Int, IsCompleted: Int,
                                   Layer: Int, PageNumber: Int, PageSize: Int, SchoolID: String,
                                   Token: String, UserID: String,
                                   SearchInfo: String): ZnbkResponse<PaperTrailInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getMobilePaperList(CourseClassID, IsRelease, IsCompleted, Layer, PageNumber,
                    PageSize, SchoolID, Token, UserID, SearchInfo)
        }
    }

    suspend fun getCatalog(token: String, userID: String,
                           schoolID: String): ZnbkResponse<MutableList<PaperCatlog>> {
        val service = ZnbkRetrofitClient.getService(SystemService::class.java, SCHOOLPAPER_URL)
        return request { service.getCatalog(token, userID, schoolID) }
    }

    suspend fun getPaperList(PageIndex: Int, PageSize: Int, PaperType: String,
                             SearchText: String): ZnbkResponse<PaperLibrary> {

        val service = ZnbkRetrofitClient.getService(PaperService::class.java, SCHOOLPAPER_URL)
        return request {
            service.getPaperList(CurrentUser.getToken(), CurrentUser.getUserID(), PaperType,
                    CurrentUser.getSchoolID(), PageIndex, PageSize, SearchText, STAGE_NO)
        }
    }

    suspend fun getClassLayerPaperCount(Layer: Int): ZnbkResponse<PaperLayerInfo> {
        val service = ZnbkRetrofitClient.getService(PaperService::class.java, BASE_URL)
        return request {
            service.getClassLayerPaperCount(CurrentUser.getToken(), CurrentUser.getUserID(),
                    Ext.COURSE_CLASSID, Layer)
        }
    }
}

